package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Department;
import com.ruoyi.system.service.IDepartmentService;
import com.ruoyi.system.util.DownloadExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 部门Controller
 * 
 * @author 胖头鱼
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseController
{
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private DownloadExcel downloadExcel;

    /**
     * 查询部门列表
     */
    @RequiresPermissions("system:department:list")
    @GetMapping("/list")
    public TableDataInfo list(Department department)
    {
        startPage();
        List<Department> list = departmentService.selectDepartmentList(department);
        return getDataTable(list);
    }

    /**
     * 导出部门列
     */
    @RequiresPermissions("system:department:export")
    @Log(title = "部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Department department)
    {
        List<Department> list = departmentService.selectDepartmentList(department);
        ExcelUtil<Department> util = new ExcelUtil<Department>(Department.class);
        util.exportExcel(response, list, "部门数据");
    }

    /**
     * 下载模版
     * @param response
     * @throws Exception
     */
//    @RequiresPermissions("system:department:exportData")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) throws Exception {
        //拿到所有数据
        List<Department> departmentList = departmentService.selectDepartmentList(null);

        downloadExcel.getDownloadExcel(response,departmentList);

    }

    /**
     * 获取部门详细信息
     */
    @RequiresPermissions("system:department:query")
    @GetMapping(value = "/{departmentId}")
    public AjaxResult getInfo(@PathVariable("departmentId") Long departmentId)
    {
        return success(departmentService.selectDepartmentByDepartmentId(departmentId));
    }

    /**
     * 新增部门
     */
    @RequiresPermissions("system:department:add")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Department department)
    {
        return toAjax(departmentService.insertDepartment(department));
    }

    /**
     * 修改部门
     */
    @RequiresPermissions("system:department:edit")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Department department)
    {
        return toAjax(departmentService.updateDepartment(department));
    }

    /**
     * 删除部门
     */
    @RequiresPermissions("system:department:remove")
    @Log(title = "部门", businessType = BusinessType.DELETE)
	@DeleteMapping("/{departmentIds}")
    public AjaxResult remove(@PathVariable Long[] departmentIds)
    {
        return toAjax(departmentService.deleteDepartmentByDepartmentIds(departmentIds));
    }
}
